
package cn.edu.nuc.creditSystem.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

import cn.edu.nuc.creditSystem.bean.BaseBean;
import cn.edu.nuc.creditSystem.bean.TreeBean;
import cn.edu.nuc.creditSystem.service.BaseService;
import cn.edu.nuc.creditSystem.utils.PageBean;

public abstract class BaseController<T extends BaseBean> {
	@SuppressWarnings("unused")
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private MessageSource messageSource;

	protected MessageSource getMessageSource() {
		return messageSource;
	}

	protected abstract BaseService<T> getService();

	protected abstract String getPrefix();

	protected String getIndex() {
		return "/index";
	}

	protected final String INDEX = getPrefix() + getIndex();

	@RequestMapping(value = "/index")
	public ModelAndView index(Model model) {
		return new ModelAndView(INDEX);
	}

	@RequestMapping(value = "/list", produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String list(T query, @RequestParam(value="page", required=false) Integer pageNumber,
			@RequestParam(value="rows", required=false) Integer pageSize, Model model) {
		PageBean page = null;
		if(pageNumber != null) {
			page = new PageBean();
			page.setCurrPage(pageNumber);
			page.setPageSize(pageSize);
		}

		List<T> list = getService().query(query, page);
		Gson gson = new Gson();
		String json = gson.toJson(list);
		
		if(pageNumber != null) {
			json = "{\"rows\":" + json + ",\"total\":" + page.getTotal() + "}";
		}
		return json;
	}

	@RequestMapping(value = "/id{id}", produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public T getById(@PathVariable("id") int id) {
		T bean = getService().get(id);
		return bean;
	}
	

	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String save(T model,HttpServletRequest request) throws Exception {
		Gson g=new Gson();
		if (model.getId() == null) {
			return g.toJson(getService().insert(model));
		} else {
			return g.toJson(getService().update(model));
		}
	}

	@RequestMapping("/delete")
	@ResponseBody
	public int delete(@RequestParam("id") int id) {
		int result = getService().delete(id);
		return result;
	}

	@RequestMapping("/updateStatus")
	@ResponseBody
	public int updateStatus(@RequestParam("ids") String ids,
			@RequestParam("status") int status) throws Exception {
		int result = getService().updateStatus(ids, status);
		return result;
	}
	@RequestMapping(value="/tree")
	@ResponseBody
	public List<TreeBean> Tree(String param){
		return getService().queryTree(param);
	}
}
